<?php
class OrderService
{
    /*
    * 添加订单
    *
    */
    public static function addrequest($user,$worker,$square,$mobile,$address,$contact_name,$start_day,$end_day)
    {
        $order = new OrderList();

        $order->address = $address;
        $order->mobile = $mobile;
        $order->contact_name = $contact_name;
        $order->square = $square;
        $order->price = $worker->price;
        $order->worker_id = $worker->worker_id;
        $order->user_id = $user->user_id;
        $order->type_id = $worker->type_id;
        $order->type_name = $worker->type_name;
        $order->start_day = $start_day;
        $order->end_day = $end_day;
        $order->amount = $order->square*$order->price;
        $order->downpay = $order->amount*MsgConst::ORDER_DOWNPAY_RATIO;
        $order->status = MsgConst::ORDER_STATUS_WAITING_ACCEPTED;

        $order->first_pay = $order->amount*MsgConst::ORDER_FIRSTPAY_RATIO;
        $order->second_pay = $order->amount*MsgConst::ORDER_SECONDPAY_RATIO;
        $order->third_pay = $order->amount*MsgConst::ORDER_THIRDPAY_RATIO;

        $detail_array = array();

        $detail_array["user_name"] = $user->real_name;
        $detail_array["avatar"]=$worker->avatar;
        $detail_array["work_year"]=$worker->work_year;
        $detail_array["worker_name"]=$worker->real_name;




        $order->detail_info = json_encode($detail_array);

        $order->order_take_time = DateUtil::getNowTime();



        Util::initObjData($order);


        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            //保存



            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }






    }


    /*
    * 拒绝接单
    *
    */
    public static function denyOrder($order,$reason)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->status = MsgConst::ORDER_STATUS_REFUSED;
            $order->deny_reason = $reason;
            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }


    /*
   * 接单
   *
   */
    public static function acceptOrder($order)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->status = MsgConst::ORDER_STATUS_ACCEPTED;
            $order->order_take_time = DateUtil::getNowTime();
           // $order->deny_reason = $reason;
            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }

    /*
  * 取消接单
  *
  */
    public static function cancelOrder($order,$reason)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->status = MsgConst::ORDER_STATUS_CANCEL;
            $order->cancel_reason = $reason;
            // $order->deny_reason = $reason;
            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }

    /*
    * 获取status信息
    *
    */
    public static function getFullStatus($status)
    {
        Util::log_outptut("status ".$status);
        //工人端  结束 包含 拒绝，取消，完成
        if($status == MsgConst::ORDER_STATUS_COMPOSED_END)
            return "('".MsgConst::ORDER_STATUS_END."','".MsgConst::ORDER_STATUS_REFUSED."','".MsgConst::ORDER_STATUS_CANCEL ."')";

        //工人端  处理中 包含 已接单 已付定金 已确认
        if($status == MsgConst::ORDER_STATUS_COMPOSED_PROCESSING)
            return "('".MsgConst::ORDER_STATUS_ACCEPTED."','".MsgConst::ORDER_STATUS_DOWNPAY."','".MsgConst::ORDER_STATUS_CONFIRMED ."')";


        //用户端 我预约的工人 等待接单  已接单  付完定金 确认完成
        if($status == MsgConst::ORDER_STATUS_COMPOSED_MY_WORKERS)
            return "('".MsgConst::ORDER_STATUS_WAITING_ACCEPTED."','".MsgConst::ORDER_STATUS_ACCEPTED."','".MsgConst::ORDER_STATUS_DOWNPAY ."','".MsgConst::ORDER_STATUS_CONFIRMED."')";


        //用户端  组合状态 已结束
        if($status == MsgConst::ORDER_STATUS_COMPOSED_ENDED)
            return "('".MsgConst::ORDER_STATUS_END."','".MsgConst::ORDER_STATUS_REFUSED."','".MsgConst::ORDER_STATUS_CANCEL ."')";







        return $status;


    }

    /*
    * 是否是单一状态
    *
    */
    public static function isSingleStatus($status)
    {
        if(strstr($status,"(")==false)
            return true;

        return false;


    }


    /*
 *支付定金
 *
 */
    public static function downpayOrder($order)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->status = MsgConst::ORDER_STATUS_DOWNPAY;
            // $order->deny_reason = $reason;
            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }



    /*
*确认定金
*
*/
    public static function confirmOrder($order,$stime,$etime)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->status = MsgConst::ORDER_STATUS_CONFIRMED;
            $order->start_day = $stime;
            $order->end_day = $etime;

            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }


    /*
*开始订单
*
*/
    public static function startOrder($order)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->status = MsgConst::ORDER_STATUS_WORKING;


            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }


    /*
*完成订单
*
*/
    public static function finishOrder($order,$rating_efficiency,$rating_quality,$rating_satisfaction,$rating_manners,$comment)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            //更新订单信息
            $order->status = MsgConst::ORDER_STATUS_END;

            //更新评分信息

            $order->rating_quality = $rating_quality;
            $order->rating_efficiency = $rating_efficiency;
            $order->rating_satisfaction = $rating_satisfaction;
            $order->rating_manners = $rating_manners;

            Util::updateObjData($order);

            Util::saveObjData($order);


            //增加订单评分
            $ratinginfo = new OrderRating();
            Util::initObjData($ratinginfo);
            $ratinginfo->order_id = $order->order_id;
            $ratinginfo->comment = $comment;
            $ratinginfo->rating_avg_efficiency = $rating_efficiency;
            $ratinginfo->rating_avg_manners = $rating_manners;
            $ratinginfo->rating_avg_quality = $rating_quality;
            $ratinginfo->rating_avg_satisfaction = $rating_satisfaction;
            $ratinginfo->user_id = $order->user_id;
            $ratinginfo->worker_id = $order->worker_id;

            $userinfo = UcUser::model()->findByPK($order->user_id);

            if(empty($userinfo)==false)
                $ratinginfo->user_mobile = $userinfo->mobile;

            Util::saveObjData($ratinginfo);

            //修改工人的评分信息

            $worker = Worker::model()->findByPK($order->worker_id);
            Util::checkExistAndNotDelete($worker);

            //评价次数＋1
            $worker->rating_num +=1;

            //评级总分
            $worker->rating_total = $worker->rating_total+intval($rating_efficiency)+intval($rating_manners)+intval($rating_quality)+intval($rating_satisfaction);

            //总平均分
            $worker->rating_avg =   $worker->rating_total / $worker-> rating_num /4;

            //4项平均分
            $worker->rating_avg_quality = OrderService::getAvgScore($worker->rating_avg_quality,($worker->rating_num-1),$rating_quality);
            $worker->rating_avg_manners = OrderService::getAvgScore($worker->rating_avg_quality,($worker->rating_num-1),$rating_manners);
            $worker->rating_avg_efficiency = OrderService::getAvgScore($worker->rating_avg_quality,($worker->rating_num-1),$rating_efficiency);
            $worker->rating_avg_satisfaction = OrderService::getAvgScore($worker->rating_avg_quality,($worker->rating_num-1),$rating_satisfaction);

            Util::updateObjData($worker);

            Util::saveObjData($worker);




            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }

    /*
     * 计算平均分
     *
     * avgs 平均分
     * num 评价数量
     * news 新的分数
     */

    public static function getAvgScore($avgs,$num,$news)
    {
        $total =  $avgs*$num+$news;

        $newavg  = $total / ($num+1);

        return $newavg;


    }





    /*
支付工程款
*
*/
    public static function payProjectOrder($order,$index)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->pay_status = $index;


            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }



    /*
申请互检
*
*/
    public static function requestOrder($order)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            $order->check_request_flag = 1;


            Util::updateObjData($order);

            Util::saveObjData($order);


            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }








}


?>